package cn.masterj.springcloud.controller;

import cn.masterj.springcloud.service.IPaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author masterJ
 * @create 2020-04-03 10:23
 */
@RestController
@RequestMapping("/consumer")
@Slf4j
//@DefaultProperties(defaultFallback = "global_fallbackHandler"
//        ,
//        commandProperties = {    // 这样配置超时也是有效的，但是这里的实际要比ribbon的超时短，才能有效，不然ribbon就会检测到超时抛异常了，而不会等到hystrix的这个配置的超时时间了
//                @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1000")
//        }
//)
public class OrderController {

    @Autowired
    private IPaymentService paymentService;

    @GetMapping("/ok")
    public String payment_ok(){
        return paymentService.payment_ok();
    }

    @GetMapping("/timeout")
//    @HystrixCommand
//     @HystrixCommand 只对单个方法起作用
//    @HystrixCommand(commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1000")
//    },defaultFallback = "fallbackHandler")
    public String payment_timeout() {
        return paymentService.payment_timeout();
    }

    @GetMapping("/exception")
//    @HystrixCommand(commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1000")
//    },defaultFallback = "fallbackHandler")
    public String payment_exception() {
        return paymentService.payment_exception();
    }

    @GetMapping("/circuit/{id}")
    public String payment_circuit(@PathVariable("id") Long id) {
        return paymentService.payment_circuit(id);
    }

    public String fallbackHandler(Throwable e){
        e.printStackTrace();
        return "我是consumer的fallbackHandler";
    }

    public String global_fallbackHandler(Throwable e){
        e.printStackTrace();
        return "我是consumer的 global_fallbackHandler";
    }
}
